//
//  HHCenterView.m
//  Flower
//
//  Created by 花花 on 2017/1/17.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "HHPopupView.h"
#import <pop/POP.h>
#import "HHPopupButton.h"
#import "HHProgressHUD.h"


@interface HHPopupView()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIButton *returnBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *returnCenterXConst;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *closeCenterXConst;
@property(copy,nonatomic)NSDictionary *buttonsInfo;

@end
@implementation HHPopupView

-(void)awakeFromNib{

    [super awakeFromNib];
 
    self.frame = [UIScreen mainScreen].bounds;
  
    [self setupUI];
    
    [self  showCurrentView];
    
    
}

-(void)setupUI{

    //如果不更新的话 则btnsView.framed打印出来的宽会默认是xib默认加载是600
    [self layoutIfNeeded];
    
    
    CGRect rect = _scrollView.bounds;
    
    
    CGFloat scrollViewW = screen_width;
    
    
    for (int i = 0 ; i < 2; i++) {
        
        CGRect offsetX = CGRectOffset(rect, i * scrollViewW, 0);
        
        UIView *btnsView =[[UIView alloc]initWithFrame:offsetX];
        
         HHLog(@"++++++++++++%@",NSStringFromCGRect(btnsView.frame));
        
        NSArray *titles = @[@"文字",@"照片/视频",@"长微博",@"签到",@"点评",@"更多"];
          NSArray *imageName = @[@"tabbar_compose_idea",@"tabbar_compose_photo",@"tabbar_compose_weibo",@"tabbar_compose_lbs",@"tabbar_compose_review",@"tabbar_compose_more"];
        [self addbtnsView:btnsView titlesArr:titles imageNamesArr:imageName ];
        
        [self.scrollView addSubview:btnsView];
        
    }
    
    self.scrollView.contentSize = CGSizeMake( screen_width * 2 , 0);
    self.scrollView.showsVerticalScrollIndicator = NO ;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.bounces = NO;
    
    self.scrollView.scrollEnabled = NO;
    

}


-(void)addbtnsView:(UIView *)btnsView titlesArr:(NSArray *)titles imageNamesArr:(NSArray *)imageNames {

    
        HHLog(@"0000000000000");

        CGSize btnSize = CGSizeMake(100, 100);
        CGFloat marginX = (screen_width - 3 * btnSize.width)/4;
        CGFloat marginY =(btnsView.height - 2 * btnSize.height)/3;
        
        for (int i = 0 ; i < imageNames.count; i++) {
     
       
            HHPopupButton *btn = [HHPopupButton addButtonTitle:titles[i] imageName:imageNames[i]];
        
        
            CGFloat row = i / 3;
            
            CGFloat btnY = (row + 1) * marginY + row * btnSize.height;
            CGFloat col = i % 3;
            CGFloat btnX = (col + 1) * marginX + col * btnSize.width;
 
            btn.frame = CGRectMake(btnX, btnY, btnSize.width, btnSize.height);
            btn.tag = i + 1000;
            
                if (btn.tag == 1005) {
                    [btn addTarget:self action:@selector(clickMore) forControlEvents:UIControlEventTouchUpInside];
                }else{
                    
                    [btn addTarget:self action:@selector(currentBtnOnClick:) forControlEvents:UIControlEventTouchUpInside];
                }

             [btnsView addSubview:btn];
            }
    
  }

#warning 点击更多的会有问题 待解决
-(void)clickMore{
    
    
    [self.scrollView setContentOffset:CGPointMake( self.scrollView.width, 0) animated:YES];
    

    _returnBtn.hidden = NO;
    
    
    CGFloat margin = self.scrollView.width /6 ;
    
    _returnCenterXConst.constant -= margin;
    _closeCenterXConst.constant += margin;
    
    [UIView animateWithDuration:0.2 animations:^{
        [self layoutIfNeeded];
    }];
    
}

-(void)currentBtnOnClick:(HHPopupButton *)btn{

  
    NSString *str =[NSString stringWithFormat:@"你点击了--%@",btn.titleLabel.text ];
    int page = _scrollView.contentOffset.x / _scrollView.width;
    
    UIView * v = _scrollView.subviews[page];
    [HHProgressHUD showStaus:str andAutoHideAfterTimer:1.0];
    
    
    for (HHPopupButton *currentBtn in v.subviews) {
        
        POPBasicAnimation *anmation =[POPBasicAnimation animationWithPropertyNamed:kPOPViewScaleXY];
        
        double scale = (btn == currentBtn) ? 1.5 : 0.8;
        
        
        anmation.toValue = [NSValue valueWithCGSize:CGSizeMake(scale, scale)];
        anmation.duration = 0.5;
        [currentBtn pop_addAnimation:anmation forKey:nil];
        
        POPBasicAnimation *aphaAnim =[POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
        aphaAnim.toValue = @(0.2);
        aphaAnim.fromValue = @(0.5);
        [currentBtn pop_addAnimation:aphaAnim forKey:nil];
        
        
        
        
    }
    

}


-(void)showCurrentView{

    POPBasicAnimation *animationAlpha = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
    animationAlpha.fromValue = 0;
    animationAlpha.toValue = @(1);
    animationAlpha.duration = 0.5;
    
    [self pop_addAnimation:animationAlpha forKey:nil];
    
    [self showButtonAnimation];
    
    
}

-(void)showButtonAnimation{

    
    UIView *firstView = self.scrollView.subviews[0];
    
    
    for (HHPopupButton *btn in firstView.subviews) {
        POPSpringAnimation *springAnimation =[POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
        springAnimation.fromValue = @(btn.center.y + 300);
        springAnimation.toValue = @(btn.center.y);
        
        springAnimation.springBounciness = 10;
        
        springAnimation.springSpeed = 10;
        
        springAnimation.beginTime = CACurrentMediaTime() *  0.025;
        [btn pop_addAnimation:springAnimation forKey:nil];
        
    }


}
- (IBAction)closeOnClick:(id)sender {
    
    
    
    int page = _scrollView.contentOffset.x / _scrollView.width;
    
    UIView * v = _scrollView.subviews[page];
    
    
    for (HHPopupButton *btn in v.subviews) {
        POPSpringAnimation *hiddenAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
        hiddenAnimation.fromValue = @(btn.center.y);
        hiddenAnimation.toValue = @(btn.center.y + 350);
        hiddenAnimation.beginTime = CACurrentMediaTime() * 0.025;
        [btn  pop_addAnimation:hiddenAnimation forKey:nil];
        
        
       
        [self hiddenCurrentView];
       
        
        
    }
    
    
    
    
}
-(void)hiddenCurrentView{
    POPBasicAnimation *animationAlpha =[POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
    
    animationAlpha.fromValue = @(1);
    animationAlpha.toValue = @(0);
    animationAlpha.duration = 0.25;
    
    [self pop_addAnimation:animationAlpha forKey:nil];
    
    [self removeFromSuperview];
    
}

- (IBAction)returnOnClick:(id)sender {
    
    [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    _returnCenterXConst.constant = 0 ;
    _closeCenterXConst.constant = 0;
    [UIView animateWithDuration:0.2 animations:^{
       
        [self layoutIfNeeded];
        self.returnBtn.alpha = 0;
    } completion:^(BOOL finished) {
        self.returnBtn.hidden = YES;
        self.returnBtn.alpha = 1;
    }];
    
    
    
    
}


@end
